package com.example.sbeducation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sbeducation.dto.HomeworkDTO;
import com.example.sbeducation.entity.Homework;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 作业Mapper接口
 */
@Mapper
public interface HomeworkMapper extends BaseMapper<Homework> {

        /**
         * 获取学生的作业列表
         * 
         * @param page      分页对象
         * @param studentId 学生ID
         * @param title     作业标题（可选）
         * @return 分页后的作业DTO列表
         */
        @Select({
                        "<script>",
                        "SELECT h.*, u.real_name AS teacher_name,",
                        "  hs.id AS submission_id, hs.status, hs.score, hs.submit_time",
                        "FROM homework h",
                        "LEFT JOIN user u ON h.teacher_id = u.id",
                        "LEFT JOIN homework_submission hs ON h.id = hs.homework_id AND hs.student_id = #{studentId}",
                        "WHERE h.deleted = 0",
                        "<if test='title != null and title.trim() != \"\"'>",
                        "  AND h.title LIKE CONCAT('%', #{title}, '%')",
                        "</if>",
                        "ORDER BY h.create_time DESC",
                        "</script>"
        })
        Page<HomeworkDTO> getStudentHomeworkList(Page<HomeworkDTO> page, @Param("studentId") Long studentId,
                        @Param("title") String title);
}